package com.ruoyi.fjtseac.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.fjtseac.mapper.PhotoMapper;
import com.ruoyi.fjtseac.domain.po.Photo;
import com.ruoyi.fjtseac.service.PhotoService;
import com.ruoyi.fjtseac.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Objects;

import static com.ruoyi.fjtseac.util.Constants.FALSE;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jxl
 * @since 2022-07-08
 */
@Service
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {


    @Value("${path.image}")
    private String imagePath;
    private final FileUtil fileUtil;

    @Autowired
    public PhotoServiceImpl(FileUtil fileUtil) {
        this.fileUtil = fileUtil;
    }

    @Override
    public AjaxResult uploadPhoto(@NotNull(message = "传输照片不能为空") MultipartFile multipartFile, String uuid) {
        String imageUrl = fileUtil.upload(multipartFile, imagePath, uuid);
        if (Objects.equals(imageUrl, FALSE)) {
            return AjaxResult.error("图片上传失败,请重新上传");
        }
        return AjaxResult.success("图片上传成功", imageUrl);
    }

    @Override
    public AjaxResult getProgress(String uuid) {
        return AjaxResult.success(fileUtil.getProgress(uuid));
    }
}